/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class ArgList extends TreeNode {

    /**
     * index of receiverDecl in child list
     */
    private static final int RECEIVERDECL_INDEX = 0;

    /**
     * index of decls in child list
     */
    private static final int DECLS_INDEX = 1;

    /** a flat list of all the non-empty argument declarations in <em>textual</em> order */
    private Decls _argDecls;

    /** a flat list of the types of the formals, in the order in which parameters should be passed */
    private List _argTypes;

    public ArgList(Location location_, Decl receiverDecl_, Decls decls_) {
        super(location_, receiverDecl_, decls_);
        constructArgDecls();
    }


    public ArgList(Decl receiverDecl_, Decls decls_) {
        super(Location.UNKNOWN, receiverDecl_, decls_);
        constructArgDecls();
    }

    /**
     * Construct a flat list of the non-empty argument declarations.
     * This method adds the decls in their left-to-right syntactic declaration
     * order (receiver, regular decls, result).
     */
    private void constructArgDecls() {
        _argDecls = new Decls();
        if (!getReceiverDecl().isEmpty()) {
            _argDecls.addDecl(getReceiverDecl());
        }
        Iterator declIter = getDecls().getDeclIter();
        while (declIter.hasNext()) {
            _argDecls.addDecl((Decl)declIter.next());
        }
    }

    /**
     * gets the receiverDecl of this
     * @return the {@link Decl}
     */
    public Decl getReceiverDecl() {
        return (Decl)childAt(RECEIVERDECL_INDEX);
    }

    /** set the ReceiverDecl of this */
    public void setReceiverDecl(Decl newChild_) {
        setChild(RECEIVERDECL_INDEX, newChild_);
    }

    /**
     * gets the decls of this
     * @return the {@link Decls}
     */
    public Decls getDecls() {
        return (Decls)childAt(DECLS_INDEX);
    }

    /** set the Decls of this */
    public void setDecls(Decls newChild_) {
        setChild(DECLS_INDEX, newChild_);
    }

    public Decls getArgDecls() {
      return _argDecls;
    }

    public List getArgTypes() {
        return _argTypes;
    }

    public void setArgTypes(List argTypes_) {
        _argTypes = argTypes_;
    }

    /** @return an iterator which enumerates the formals <em>in order</em>,
        including proper placement of the result parameter */
    public InOrderFormalIter orderedIter() {
        return new InOrderFormalIter();
    }

    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

    public String nodeString() {
        return getReceiverDecl().nodeString() + ", " + getDecls().nodeString();
    }

    public String toString() {
        return nodeString();
    }

    /**
     * an Iterator which gives an in-order iteration of the formals
     */
    public class InOrderFormalIter implements Iterator {

        private boolean _didReceiver = false;
        private Iterator _mainDeclIter = getDecls().getDeclIter();
        private Decl _curMainDecl = null;
        private Decl _curDecl = null;
        private Iterator _curVarIter = null;

        public boolean hasNext() {
            if (!_didReceiver && !getReceiverDecl().isEmpty()) {
                return true;
            }
            return (_curVarIter != null && _curVarIter.hasNext()) || _mainDeclIter.hasNext();
        }

        /** @return next Variable */
        public Object next() {
            Variable ret = null;
            if (!_didReceiver) {
                Decl receiverDecl = getReceiverDecl();
                if (!receiverDecl.isEmpty()) {
                    _curDecl = receiverDecl;
                    // we know there will only be one variable
                    ret = (Variable)receiverDecl.getVariables().childAt(0);
                } else if (_mainDeclIter.hasNext()) {
                    ret = _newMainDecl();
                }
                _didReceiver = true;
            }
            if (ret == null) {
                if (_curVarIter != null && _curVarIter.hasNext()) {
                    _curDecl = _curMainDecl;
                    ret = (Variable)_curVarIter.next();
                } else {
                    ret = _newMainDecl();
                }
            }
            if (ret == null) {
                throw new NoSuchElementException();
            }
            return ret;
        }

        private Variable _newMainDecl() {
            if (_mainDeclIter.hasNext()) {
                _curMainDecl = (Decl)_mainDeclIter.next();
                _curDecl = _curMainDecl;
                _curVarIter = _curMainDecl.getVariables().getVariableIter();
                return (Variable)_curVarIter.next();
            } else {
                return null;
            }
        }

        public Variable nextVar() {
            return (Variable)next();
        }

        public Decl getCurDecl() {
            return _curDecl;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

    }
}


